# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=gmsh
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.13.1
pkgrel=4
pkgdesc="3D finite element mesh generator with built-in CAD engine and post-processor (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gmsh.info"
msys2_repository_url="https://gitlab.onelab.info/gmsh/gmsh/"
license=("spdx:GPL-2.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-petsc"
             "${MINGW_PACKAGE_PREFIX}-slepc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-med"
         "${MINGW_PACKAGE_PREFIX}-opencascade"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://gmsh.info/src/gmsh-${pkgver}-source.tgz
        0001-fix-link-lib-name.patch
        0002-fix-dynamic-linking.patch
        0003-fltk-1.4.patch::https://gitlab.onelab.info/gmsh/gmsh/-/commit/3b3f0f7e16430939b345889a9e31b50104d5baf3.patch
        0004-cmake-4.0.patch::https://gitlab.onelab.info/gmsh/gmsh/-/commit/499461f6d57b156bc76a8fdc607097426a8545a5.patch
        0005-aarch64-image-base.patch)
sha256sums=('77972145f431726026d50596a6a44fb3c1c95c21255218d66955806b86edbe8d'
            'e575ae8fc757694affb6749dc9d52c336691b06159845655ef2ac27aebae8159'
            '4d3f5c4190529e3d45b786adab83f0d65e726357d7367f682ff95b733a88a8c3'
            'f06d4ce2d45b640531d4556fb3fa41741903a4b27a2f28b1d3a1a82be77a42d0'
            '7b692c1e78168d7fc43b451a34c104079819fe89e80c97f5ba80005040389b0c'
            '67a06fe851d807d5ba73cfaa32e323ff7fadb4334d19b81d1933e7e8bf21ed22')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/gmsh-${pkgver}-source"

  apply_patch_with_msg \
    0001-fix-link-lib-name.patch \
    0002-fix-dynamic-linking.patch \
    0003-fltk-1.4.patch \
    0004-cmake-4.0.patch

  if [[ ${CARCH} == aarch64 ]]; then
    apply_patch_with_msg \
      0005-aarch64-image-base.patch

    # The icons are only provided in binary format for 32 bits
    # and 64bits without source code
    msg2 "Creating aarch64 icon resource binary"

    echo "#define IDI_ICON 101" >> src/fltk/Win64Icon.h
    echo "#include \"Win64Icon.h\"" >> src/fltk/Win64Icon.rc
    echo "IDI_ICON ICON \"Win64Icon.ico\"" >> src/fltk/Win64Icon.rc

   "${MINGW_PREFIX}"/bin/windres  \
    -i "src/fltk/Win64Icon.rc" \
    -o "src/fltk/Win64Icon.res"
  fi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DENABLE_BUILD_DYNAMIC=ON \
      -DENABLE_BUILD_SHARED=ON \
      -DENABLE_CAIRO=ON \
      -DENABLE_OCC=ON \
      -DENABLE_OPENMP=ON \
      -DENABLE_OPENACC=ON \
      -DENABLE_PETSC=ON \
      -DENABLE_SLEPC=ON \
      -DGMSH_RELEASE=ON \
      ../gmsh-${pkgver}-source

  "${MINGW_PREFIX}"/bin/cmake --build .
}

check () {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/ctest .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake --install .

  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
}
